//
//  YDBProgressHUD.swift
//  基础框架
//
//  Created by 杨东宝 on 2018/7/26.
//  Copyright © 2018年 杨东宝. All rights reserved.
//

import UIKit

enum HUDType {
    case success
    case error
    case loading
    case info
    case progress
}

class YDBProgressHUD: NSObject {
    
     let progressHUD = SVProgressHUD()
     class func initYDBProgressHUD() {
        SVProgressHUD.setFont(UIFont.systemFont(ofSize: 14.0))
        SVProgressHUD.setDefaultMaskType(.none)
        SVProgressHUD.setMinimumDismissTimeInterval(2.5)
       
    }
    
    class func showSuccess(_ status: String) {
        self.showYDBProgressHUD(type: .success, status: status)
    }
    class func showError(_ status: String) {
        self.showYDBProgressHUD(type: .error, status: status)
    }
    class func showLoading(_ status: String) {
        self.showYDBProgressHUD(type: .loading, status: status)
    }
    class func showInfo(_ status: String) {
        self.showYDBProgressHUD(type: .info, status: status)
    }
    class func showProgress(_ status: String, _ progress: CGFloat) {
        self.showYDBProgressHUD(type: .success, status: status, progress: progress)
    }
    class func dismissHUD(_ delay: TimeInterval = 0) {
        SVProgressHUD.dismiss(withDelay: delay)
    }
}


extension YDBProgressHUD {

    class func showYDBProgressHUD(type: HUDType, status: String, progress: CGFloat = 0) {
        
        let hud = SVProgressHUD()
        
        if #available(iOS 12.0, *) {
            if hud.traitCollection.userInterfaceStyle == UIUserInterfaceStyle.light {
                
                SVProgressHUD.setDefaultStyle(.dark)
                
            }else{
                
                SVProgressHUD.setDefaultStyle(.light)
            }
        } else {
            SVProgressHUD.setDefaultStyle(.dark)
        }

        SVProgressHUD.setFont(UIFont.systemFont(ofSize: 14.0))
        SVProgressHUD.setDefaultMaskType(.none)
        SVProgressHUD.setMinimumDismissTimeInterval(2.5)
        SVProgressHUD.setContainerView(kAppWindow())
        switch type {
        case .success:
            SVProgressHUD.showSuccess(withStatus: status)
        case .error:
            SVProgressHUD.showError(withStatus: status)
        case .loading:
            SVProgressHUD.show(withStatus: status)
        case .info:
            SVProgressHUD.showInfo(withStatus: status)
        case .progress:
            SVProgressHUD.showProgress(Float(progress), status: status)
        }
    }
}
